package com.be.beadmin.project.controller;

import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.exception.BeadminException;
import com.be.beadmin.common.utils.ResultModel;
import com.be.beadmin.project.dto.input.TeamDto;
import com.be.beadmin.project.dto.output.ProjectInfoSimpEx;
import com.be.beadmin.project.service.TeamService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
* @author zjm
* @date 2023-02-02
**/
@Api(tags = {"项目管理-团队模块"})
@RestController
public class TeamController {

    @Autowired
    private  TeamService teamService;

    @ApiOperation("获取团队分页信息")
    @PostMapping(value = "/project/getTeamByPage")
    public ResultModel getTeamByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(teamService.getTeamByPage(pageEntity)) ;
    }

    @ApiOperation("获取所有团队详细信息")
    @PostMapping(value = "/project/getTeamInfos")
    public ResultModel getTeamInfos()  {
        return ResultModel.success(teamService.getTeamInfos()) ;
    }

    @ApiOperation("根据项目ID获取该项目团队信息")
    @PostMapping(value = "/project/getProjectTeamInfos")
    public ResultModel getProjectTeamInfos(@ApiParam(value = "项目ID") @RequestParam String infoId)  {
        try {
            return ResultModel.success(teamService.getProjectTeamInfos(infoId)) ;
        } catch (BeadminException e) {
            return ResultModel.error(-1,e.getMessage()) ;
        }
    }

    @ApiOperation("根据项目ID和团队ID更换项目团队")
    @PostMapping(value = "/project/changeProjectTeam")
    public ResultModel changeProjectTeam(@ApiParam(value = "项目ID") @RequestParam String infoId,
                                         @ApiParam(value = "团队ID") @RequestParam String teamId)  {
        try {
            return ResultModel.success(teamService.changeProjectTeam(infoId,teamId)) ;
        } catch (BeadminException e) {
            return ResultModel.error(-1,e.getMessage()) ;
        }
    }



    @ApiOperation("获取项目团队关联分页信息")
    @PostMapping(value = "/project/getTeamAndProjectByPage")
    public ResultModel getTeamAndProjectByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(teamService.getTeamAndProjectByPage(pageEntity)) ;
    }

    @ApiOperation("获取指定id的团队信息")
    @PostMapping(value = "/project/getTeamByIdAndDelete")
    public ResultModel getTeamByIdAndDelete(@RequestParam String teamId)  {
        return ResultModel.success(teamService.getTeamByIdAndDelete(teamId)) ;
    }



    @ApiOperation("获取所有项目的简易信息")
    @PostMapping("/project/getProjectInfoSimp")
    public ResultModel getProjectInfoSimp(){
        List<ProjectInfoSimpEx> projectInfoSimps = teamService.getProjectInfoSimp();
        return ResultModel.success(projectInfoSimps);
    }

    @ApiOperation("添加一个团队")
    @PostMapping("/project/addTeam")
    public ResultModel addTeam(@RequestBody TeamDto teamDto ){
//        System.out.println("teamDto:"+teamDto);
        int result = teamService.addTeam(teamDto);
        if (result == -1 ){
            return ResultModel.success("["+ teamDto.getTeamName()+"]团队已存在，请勿重复添加");
        }else
        return ResultModel.success("["+ teamDto.getTeamName()+"]团队添加成功");
    }

    @ApiOperation("删除一个团队")
    @PostMapping("/project/delTeam")
    public ResultModel delTeam(@RequestBody TeamDto teamDto){
        teamService.delTeam(teamDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }

    @ApiOperation("修改一个团队的信息")
    @PostMapping("/project/updateTeam")
    public ResultModel updateTeam(@RequestBody TeamDto teamDto){
        int result = teamService.updateTeam(teamDto);
//        return ResultModel.success(ResultModel.UPDATE);
        if (result == -1 ){
            return ResultModel.error(-1,"["+ teamDto.getTeamName()+"]项目id和项目名不可修改");
        }else
            return ResultModel.success("["+ teamDto.getTeamName()+"]团队信息更改成功");
    }
}